import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import { getAccountPageList, getAccountInfo, createAccount, updateAccount, deleteAccount } from "../../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>): DataAction => ({
    fetchList: (params) => {
        const { page, pageSize, filter } = params;
        return new Promise((resolve, reject) => getAccountPageList(Object.assign({page,pageSize}, filter || {})).then((resp) => resolve(resp.data)).catch((e) => reject(e)))
    },
    getItem: (rowKey) => {
        const accountId = rowKey as string;
        return new Promise((resolve, reject) => getAccountInfo(accountId).then((resp) => {
            resolve({
                accountName: resp.data.accountName,
                address: resp.data.address,
                industry: resp.data.industry,
                linkman: resp.data.linkman,
                phone: resp.data.phone,
                remark: resp.data.remark,
            });
        }).catch((e) => reject(e)));
    },
    createItem: (params) => {
        return new Promise((resolve, reject) => createAccount(params).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    updateItem: (rowKey, params) => {
        return new Promise((resolve, reject) => updateAccount(rowKey as string, params).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    deleteItems: (selectedRowKeys) => {
        return deleteAccount(selectedRowKeys)
    },
})


